package com.dullwolf.utils;


import com.dullwolf.common.Constant;
import com.jfinal.kit.StrKit;
import io.jboot.Jboot;
import io.jboot.component.redis.JbootRedis;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CmdUtil {

    private static JbootRedis redis = Jboot.me().getRedis();

    private static final int MESSAGE_TYPE_PRIVATE = 0;
    private static final int MESSAGE_TYPE_GROUP = 1;
    private static final int MESSAGE_TYPE_DISCUSS = 2;


    public static String reset(long userId, long id, int type, String osuName) {
        if("".equals(osuName)){
            //判断参数为空的情况，就找redis有没有记录
            String username = redis.hget(Constant.USERTAG + userId, "username");
            if(StrKit.isBlank(username)){
                CmdUtil.sendMessage("请在命令后面输入您游戏的名字吧，!help可查看帮助", id, type);
            }else{
                //用户曾经输入过命令，还需要判断是不是在群里发出的命令
                if(MESSAGE_TYPE_GROUP == type){
                    Map<Object,Object> map = new HashMap<>();
                    map.put(String.valueOf(userId),username);
                    redis.hmset(Constant.USEGROUPTAG + id,map);
                }
            }
        }else{
            //不管用户第几次输入osuName，重新set进redis
            Map<Object,Object> map = new HashMap<>();
            map.put("username",osuName);
            redis.hmset(Constant.USERTAG + userId,map);
            //同时判断是群里发出来的，就添加一个群用户标记
            if(MESSAGE_TYPE_GROUP == type){
                map = new HashMap<>();
                map.put(String.valueOf(userId),osuName);
                redis.hmset(Constant.USEGROUPTAG + id,map);
            }

        }
        return redis.hget(Constant.USERTAG + userId, "username");
    }


    public static void sendMessage(String msg, long id, int type) {
        switch (type) {
            case MESSAGE_TYPE_PRIVATE:
                CqUtil.sendMsg(id, msg, "private");
                break;
            case MESSAGE_TYPE_GROUP:
                CqUtil.sendMsg(id, msg, "group");
                break;
            case MESSAGE_TYPE_DISCUSS:
                CqUtil.sendMsg(id, msg, "discuss");
                break;
            default:
                break;
        }
    }

    public static String getModeName(int mode) {
        String modes = "";
        if (0 == mode) {
            modes = "std";
        } else if (1 == mode) {
            modes = "Taiko";
        } else if (2 == mode) {
            modes = "CtB";
        } else if (3 == mode) {
            modes = "mania";
        }
        return modes;
    }



    public static String getParams(String params ,int num){
        String param = "";
        if(StringUtils.isNotBlank(params)){
            String trim = params.substring(1).trim();
            if(StringUtils.isNotBlank(trim)){
                param = trim.substring(num).trim();
                param = param.replaceAll("&#91;", "[");
                param = param.replaceAll("&#93;", "]");
                param = param.replaceAll("&#44;", ",");
                param = param.replaceAll("&amp;", "&");
            }
        }
        return param;
    }


    public static String getTrimParams(String msg) {
        Pattern p = Pattern.compile("\\s+");
        Matcher m = p.matcher(msg);
        return m.replaceAll(" ").trim();
    }

    public static String getLowerParams(String msg) {
        Pattern p = Pattern.compile("\\s+");
        Matcher m = p.matcher(msg);
        return m.replaceAll(" ").toLowerCase().trim();
    }
}
